package com.sgcc.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.sgcc.common.core.utils.StringUtils;
import com.sgcc.system.api.domain.SysDistrict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.system.service.ISysDeptService;
import com.sgcc.system.service.ISysDistrictService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * SysDistrictController
 * 
 * @author sgcc
 * @date 2021-03-22
 */
@RestController
@RequestMapping("/district")
public class SysDistrictController extends BaseController
{
    @Autowired
    private ISysDistrictService sysDistrictService;
    

    /**
     * 查询SysDistrict列表
     */
    //@PreAuthorize(hasPermi = "system:district:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDistrict sysDistrict)
    {
        startPage();
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        return getDataTable(list);
    }

    /**
     * 导出SysDistrict列表
     */
    //@PreAuthorize(hasPermi = "system:district:export")
    @Log(title = "SysDistrict", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDistrict sysDistrict) throws IOException
    {
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        ExcelUtil<SysDistrict> util = new ExcelUtil<SysDistrict>(SysDistrict.class);
        util.exportExcel(response, list, "district");
    }

    /**
     * 获取SysDistrict详细信息
     */
    //@PreAuthorize(hasPermi = "system:district:query")
    @GetMapping(value = "/{districtId}")
    public AjaxResult getInfo(@PathVariable("districtId") String districtId)
    {
        return AjaxResult.success(sysDistrictService.selectSysDistrictById(districtId));
    }

    /**
     *  根据orgId获取对应的districtId编号
     * */
    @RequestMapping(value = "/getDistrictByOrgId")
    public AjaxResult getDistrictByOrgId(String orgId, @RequestParam("isContain") String isContain)
    {
        SysDistrict sysDistrict = new SysDistrict();
        sysDistrict.setIsContain(isContain);
        if (StringUtils.isEmpty(orgId)){
            return AjaxResult.success("orgId无值");
        }
        sysDistrict.setDistrictId(orgId);
        List<SysDistrict> sysDistrictList = sysDistrictService.getDistrictByOrgId(sysDistrict);
        return AjaxResult.success(sysDistrictList);
    }

    /**
     * 新增SysDistrict
     */
    //@PreAuthorize(hasPermi = "system:district:add")
    @Log(title = "SysDistrict", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDistrict sysDistrict)
    {
    	SysDistrict district=sysDistrictService.findOrgId(sysDistrict);
    	if(null==district) {
    		return toAjax(sysDistrictService.insertSysDistrict(sysDistrict));
    	}else {
    		return AjaxResult.error("不能选择相同部门进行映射~");
    	}
        
    }

    /**
     * 修改SysDistrict
     */
    //@PreAuthorize(hasPermi = "system:district:edit")
    @Log(title = "SysDistrict", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDistrict sysDistrict)
    {
        SysDistrict district=sysDistrictService.findOrgId(sysDistrict);
    	if(null==district) {
    		return toAjax(sysDistrictService.updateSysDistrict(sysDistrict));
    	}else {
    		return AjaxResult.error("不能选择相同部门进行映射~");
    	}
    }

    /**
     * 删除SysDistrict
     */
    //@PreAuthorize(hasPermi = "system:district:remove")
    @Log(title = "SysDistrict", businessType = BusinessType.DELETE)
	@DeleteMapping("/{districtIds}")
    public AjaxResult remove(@PathVariable String[] districtIds)
    {
        return toAjax(sysDistrictService.deleteSysDistrictByIds(districtIds));
    }

}
